package com.example.service;

import com.example.entity.Admin;
import com.example.entity.Advice;
import com.example.entity.Stayinfo;
import com.example.mapper.AdviceMapper;
import com.example.mapper.StayinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdviceService {
    @Autowired
    private AdviceMapper adviceMapper;
//    public int save(Advice advice) {
//        if (existsInDb(advice)) {
//            return adviceMapper.update(advice);
//        } else {
//            return adviceMapper.insert(advice);
//        }
//    }
public boolean existsInDb1(Advice advice){
    List<Advice> adminList = adviceMapper.findByid(advice.getAdvice_id());
    return !adminList.isEmpty();
}

    public int save1(Advice advice){
        if(existsInDb1(advice) ) {
            return adviceMapper.update(advice);
        }else {
            return adviceMapper.insert(advice);
        }
    }


    public Map<String, Object> saveAdvice(Advice advice) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save1(advice);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

    public Map<String, Object> deleteByAdviceId(Integer adviceId) {
        Map<String, Object> result = new HashMap<>();

        Integer deleteResult = adviceMapper.deleteBy(adviceId);

        if (deleteResult != null) {
            result.put("code", 200);
            result.put("message", "删除成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "删除失败");
            result.put("data", null);
        }

        return result;
    }
}
